package yxy.apple.util;

import jodd.util.StringUtil;

import java.util.*;

//渠道SDK接入要用到的功能
public class SdkUtil {

    public static String mapToContent (Map<String, String> params, boolean includeEmptyParam) {
        StringBuilder sb = new StringBuilder();
        // 按照key做排序
        List<String> keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);
        for (String key : keys) {
            String value = params.get(key);
            //拼接成key=value&key=value&....格式的字符串
            if (StringUtil.isEmpty(value) && !includeEmptyParam) continue; //空值不处理
            if (sb.length() > 0) sb.append("&");
            sb.append(key).append("=").append(value);
        }
        return sb.toString();
    }

    public static String treeMapToContent (Map<String, String> params, boolean includeEmptyParam) {
        Set<String> keys = params.keySet();
        StringBuilder sb = new StringBuilder();
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            String value = params.get(key);
            //拼接成key=value&key=value&....格式的字符串
            if (StringUtil.isEmpty(value) && !includeEmptyParam) continue; //空值不处理
            if (sb.length() > 0) sb.append("&");
            sb.append(key).append("=").append(value);
        }
        return sb.toString();
    }
}
